import {Component} from '@angular/core';
import {App, IonicPage, Nav, NavController, NavParams, Platform} from "ionic-angular";

import {AuthService} from "../../services/common/auth.service";
import {ActivityListPage} from "../activity/activity-list/activity-list";
import {IndexPage} from "../index";
import * as _ from 'lodash';

interface PageInterface {
    name: string;
    index?: number;
}

@IonicPage()
@Component({
    selector: 'home_tabs',
    templateUrl: 'tabs.html'
})
export class TabsPage {

    tab0Root = 'IndexPage';
    tab1Root = 'ClientIndexPage';
    tab2Root = 'PolicyListPage';
    tab3Root = 'ActivityListPage';
    tab4Root = 'MePage';
    tab5Root = 'MessageOverviewPage';
    mySelectedIndex: number;

    constructor(public appCtrl: App,
                public navCtrl: NavController,
                public navParams: NavParams,
                private nav: Nav,
                private platform: Platform,
                public authService: AuthService,) {
        this.mySelectedIndex = navParams.data.tabIndex || 0;
        this.pages = this.authService.getNavMenu();
    }
    pages = [];

    /**
     * 跳转tabs根路径同时设置图标高量
     * @param {PageInterface} page
     */
    public openPage(page: PageInterface) {
        let params = {};

        // the nav component was found using @ViewChild(Nav)
        // setRoot on the nav to remove previous pages and only have this page
        // we wouldn't want the back button to show in this scenario
        if (page.index) {
            params = {tabIndex: page.index};
        }

        // If we are already on tabs just change the selected tab
        // don't setRoot again, this maintains the history stack of the
        // tabs even if changing them from the menu
        if (this.nav.getActiveChildNavs().length && page.index != undefined) {
            this.nav.getActiveChildNavs()[0].select(page.index);
            // Set the root of the nav with params if it's a tab index
        } else {
            this.nav.setRoot(page.name, params).catch((err: any) => {
                console.log(`Didn't set nav root: ${err}`);
            });
        }
    }
}
